package net.bwie.realtime.jtp.dwd.order.job;

import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.jtp.dwd.order.utils.HbaseUtil;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.configuration.Configuration;

import java.util.Map;

/**
 * 简介说明:
 *
 * @author: LiLi
 * @date: 2025/06/03 21:30:34
 * @version: 1.0
 */
public class LoadProvinceDimMapFunction extends RichMapFunction<String, String> {

    /**
     *  定义map的结果，key为关联字段
     */
    private Map<String, String> cacheMap ;

    @Override
    public void open(Configuration parameters) throws Exception {
        // 获取HBase表中省份维度数据，存储到Map集合中
         cacheMap = HbaseUtil.scanData("dim_base_province", "info", "name");
    }

    @Override
    public String map(String value) throws Exception {

        JSONObject jsonObject = JSONObject.parseObject(value);

        String provinceId = jsonObject.getString("province_id");

        String provinceName = cacheMap.getOrDefault(provinceId, "unknown");
        // 添加字段
        jsonObject.put("province_name", provinceName);

        return jsonObject.toJSONString();
    }

    @Override
    public void close() throws Exception {
        super.close();
    }
}
